<template>
	<view class="wrap">
		<!-- 轮播图 -->
		<swiper circular :indicator-dots="true" autoplay :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item,index) in goodsDetail.pics" :key="index">
				<image :src="item.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>  
		<!-- 商品信息区 -->
		<view class="goods-info">
			<view class="goods-free" v-if="goodsDetail.goods_price">¥ {{goodsDetail.goods_price}}</view>
			<view class="goods-shouc">
				<view class="goods-text">{{goodsDetail.goods_name}}</view>
				<view class="goods-sc">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="goods-kd">快递:免费</view>
		</view>
		<!-- 商品详情信息 -->
		<rich-text :nodes="goodsDetail.goods_introduce" class="rich-text"></rich-text>
	</view>
	<!-- GoodsNav -->
	<view class="goods-carts">
		<uni-goods-nav 
		:options="options" 
		:fill="true" 
		:button-group="buttonGroup" 
		@click="onClick"
		@buttonClick="buttonClick" 
		/>
	</view>
</template>

<script>
	import { mapActions,mapState,mapMutations,mapGetters } from 'vuex'
	export default {
		data() {
			return {
				goodsDetail:{},
				options: [     
							{
								icon: 'shop',
								text: '店铺',
								infoBackgroundColor: '#007aff',
								infoColor: "#f5f5f5"
							}, {
								icon: 'cart',
								text: '购物车',
								info: 0
							},
						],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
						color: '#fff'
					}],
			   }
		},
		computed:{
			...mapState({
				text: state => state.cart.text,
				cart:state =>state.cart.cartInfo
				// timestamp: state => state.moduleB.timestamp
			}),
			...mapGetters(['total'])
		},
		watch:{
			total:{
				handler(value){
					let findResult= this.options.find((item)=>item.text ==='购物车')
					findResult.info = value;
				},
				immediate: true
			}
			
		},
		onLoad(option) {
			const goods_id = option.goods_id
			this.getDoodsDetail(goods_id)
		},
		methods:{
			...mapMutations(['addToCart']),
			//点击购物车
			onClick(e) {
				if(e.content.text ==='购物车'){
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
			},
			//点击加入购入车
			buttonClick(e) {
				if(e.content.text === '加入购物车'){
					let goods = {
						 goods_id: this.goodsDetail.goods_id,       // 商品的Id
						 goods_name: this.goodsDetail.goods_name,   // 商品的名称
						 goods_price: this.goodsDetail.goods_price, // 商品的价格
						 goods_count: 1,                           // 商品的数量
						 goods_small_logo: this.goodsDetail.goods_small_logo, // 商品的图片
						 goods_state: true                         // 商品的勾选状态
					}
					this.addToCart(goods)
				}
				
			},
			 async getDoodsDetail(goods_id){
				 const {data:res} = await uni.$http.get('/api/public/v1/goods/detail',{goods_id})
				 if(res.meta.status !== 200) return uni.showMsg()
				 res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g,'<img style="display:block;" ').replace(/webp/g, 'jpg');
				 this.goodsDetail = res.message
			 },
			 preview(i){
				 uni.previewImage({
				 	current:i,
					urls:this.goodsDetail.pics.map(x=>x.pics_big)//返回的是所有图片的 url 数组
				 })
			 }
		}
	}
</script>

<style lang="scss">
swiper{
	height: 750rpx;
	image{
		width:100%;
		height:100%;
	}
}

.goods-info{
	padding: 0 10px;
	.goods-free{
		font-size:18px;
		color: #c00000;
		padding:10px 0;
	}
	.goods-shouc{
		display: flex;
		.goods-text{
			// padding:0 8px;
			width:80%;
			font-size:14px;
			// background-color: yellow;
			border-right:1px solid #efefef;
		}
		.goods-sc{
			display: flex;
			flex-direction:column;
			justify-content: center;
			align-items: center;
			flex:1;
			font-size: 12px;
			color:gray;
			// background-color: yellow;
		}
	}
	.goods-kd{
		margin-top:10px;
		font-size:12px;
		color:gray;
	}
}
.wrap{
	margin-bottom:50px;
}
.goods-carts{
	position: sticky;
	// position: fixed;
	bottom:50px;
}
</style>
